//
//  Reg1ViewController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/12/6.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "Reg1ViewController.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"
#import "Reg2ViewController.h"




@interface Reg1ViewController ()<UITextFieldDelegate>
@property(nonatomic,strong) UIImageView *image1;
@property(nonatomic,strong) UITextField *textPhone;
@property(nonatomic,strong) UITextField *textCode;
@property(nonatomic,strong) UITextField *textpassword1;
@property(nonatomic,strong) UITextField *textpassword2;
@property(nonatomic,strong) UIButton *btnCode;
@property(nonatomic,strong)  UILabel *countLabel;

@property(nonatomic,strong)NSTimer *timer;



@property(nonatomic,strong) UIButton *btnNext;

//网路请求
@property(nonatomic,strong)AFHTTPSessionManager *manager;


@end


static NSInteger count;

@implementation Reg1ViewController



//隐藏tabbar
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}


//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"注  册";
    self.titleLabel.font = [UIFont boldSystemFontOfSize:19];
    self.backBtn.hidden = NO;
    self.view.backgroundColor = BACKGROUND1;
    
    
    
    [self initView];
 
    
    
}



-(void)back:(id)sender
{

    [self dismissViewControllerAnimated:YES completion:nil];


}

-(void)initView
{



    self.image1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 64+40, ViewWidth-20, 38)];
    self.image1.image = [UIImage imageNamed:@"register_1"];
    [self.view addSubview:self.image1];
    
    
    
    
    //设置前后的空白位置
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 64+40+38+30, 10, 50)];
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view2];
    
    UIView *view23 = [[UIView alloc]initWithFrame:CGRectMake(ViewWidth-15, 64+40+38+30, 15, 50)];
    view23.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view23];
    
    
    self.textPhone=[[UITextField alloc]initWithFrame:CGRectMake(10, 64+40+38+30, ViewWidth-10-15, 50)];
    self.textPhone.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.textPhone];
    self.textPhone.delegate = self;
    self.textPhone.keyboardType = UIKeyboardTypeNumberPad;
    self.textPhone.clearButtonMode = UITextFieldViewModeAlways;
    self.textPhone.placeholder = @"请输入手机号";
    
    
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(10, 64+40+38+30+49, ViewWidth-10, 1)];
    view1.backgroundColor = BACKGROUND1;
    [self.view addSubview:view1];

    
    
    
    
    //设置前后的空白位置
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 64+40+38+30+50, 10, 50)];
    view3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view3];
    
//    UIView *view33 = [[UIView alloc]initWithFrame:CGRectMake(ViewWidth-15, 64+40+38+30+50, 15, 50)];
//    view33.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:view33];
    
    
    self.textCode =[[UITextField alloc]initWithFrame:CGRectMake(10, 64+40+38+30+50, ViewWidth-10-15-100, 50)];
    self.textCode.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.textCode];
    self.textCode.delegate = self;
    self.textCode.keyboardType =UIKeyboardTypeNumberPad;
    self.textCode.clearButtonMode = UITextFieldViewModeAlways;
    self.textCode.placeholder = @"请输入验证码";
    
    
    self.btnCode = [[UIButton alloc]initWithFrame:CGRectMake(ViewWidth-10-15-100+10+1,64+40+38+30+50 , 99+15+10, 50)];
    [self.btnCode setTitle:@"获取验证码  " forState:UIControlStateNormal];
    [self.view addSubview:self.btnCode];
    self.btnCode.titleLabel.font =[UIFont boldSystemFontOfSize:17];
    self.btnCode.backgroundColor  = [UIColor whiteColor];
    [self.btnCode setTitleColor:BenSe_COLOR forState:UIControlStateNormal];
    

    [self.btnCode addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    //添加活动的数据label
    self.countLabel = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth-10-15-100+10+1,64+40+38+30+50 , 99+15+10, 50)];
    [self.view addSubview:self.countLabel];
    self.countLabel.hidden = YES;

    
    //设置提现密码
    
    
    //设置前后的空白位置
    UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(0, 64+40+38+30+50*2+10, 10, 50)];
    view4.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view4];
    
    self.textpassword1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 64+40+38+30+50*2+10, ViewWidth-10, 50)];
    self.textpassword1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.textpassword1];
    self.textpassword1.secureTextEntry = YES;
    self.textpassword1.delegate = self;
    self.textpassword1.placeholder  = @"请输入密码";
    self.textpassword1.returnKeyType = UIReturnKeyDone;
    
    
    
    
    
    
    //设置前后的空白位置
    UIView *view5 = [[UIView alloc]initWithFrame:CGRectMake(0, 64+40+38+30+50*2+10, 10, 50)];
    view5.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view5];
    
    //中间的线
    UIView *view6 = [[UIView alloc]initWithFrame:CGRectMake(10, 64+49+40+38+30+50*2+10, ViewWidth-10-10, 1)];
    view6.backgroundColor = BACKGROUND1;
    [self.view addSubview:view6];
    
    
    self.textpassword2 = [[UITextField alloc]initWithFrame:CGRectMake(10, 64+40+38+30+50*2+10+50, ViewWidth-10, 50)];
    self.textpassword2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.textpassword2];
    self.textpassword2.secureTextEntry = YES;
    self.textpassword2.delegate = self;
    self.textpassword2.placeholder = @"请再次输入密码";
    self.textpassword2.returnKeyType = UIReturnKeyDone;
    

    
    
    
    
    
    //设置前后的空白位置
    UIView *view7= [[UIView alloc]initWithFrame:CGRectMake(0, 64+40+38+30+50*2+10+50, 10, 50)];
    view7.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view7];


    
    
    //下一步
    self.btnNext = [[UIButton alloc]initWithFrame:CGRectMake(20, 64+40+38+30+50*2+10+50+50+20, ViewWidth-40, 50)];
    [self.view addSubview:self.btnNext];
    self.btnNext.backgroundColor = [UIColor whiteColor];
    [self.btnNext setTitle:@"下一步" forState:UIControlStateNormal];
    self.btnNext.titleLabel.font = [UIFont boldSystemFontOfSize:19];
    [self.btnNext setTitleColor:BenSe_COLOR forState:UIControlStateNormal];
    
    [self.btnNext.layer setMasksToBounds:YES];
    [self.btnNext.layer setCornerRadius:3.0];
    
    [self.btnNext addTarget:self action:@selector(next2:) forControlEvents:UIControlEventTouchUpInside];


}

#pragma mark 跳转到下一个注册页面
-(void)next2:(id)sender
{
    
    
    [self.textPhone resignFirstResponder];
    [self.textCode resignFirstResponder];
    [self.textpassword1 resignFirstResponder];
    [self.textpassword2 resignFirstResponder];
    
    

    if (self.textPhone.text.length == 0) {
        [self.view makeToast:@"手机号码不能为空"];
        return;
    }
    if (self.textCode.text.length == 0) {
        
        [self.view makeToast:@"验证码不能为空"];
        return;
        
    }
    if (self.textpassword1.text.length == 0) {
        [self.view makeToast:@"密码不能为空"];
        return;
    }
    if (![self.textpassword1.text isEqualToString:self.textpassword2.text]) {
        [self.view makeToast:@"两次输入不一致"];
        return;
        
    }
    
    
    //网络请求验证手机号以及验证码是否正确
    NSString *urlStr = [NSString stringWithFormat:@"%@check_wholesaler_code",SERVER_ADDRESS];
    NSDictionary *parmas = @{@"wTel" : self.textPhone.text ,@"code" : self.textCode.text};
    
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.manager POST:urlStr parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"error_flag"] intValue] == 0) {
            
            
            
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
         
            
            //MD5加密
            NSString *MM = md5(self.textpassword1.text);
            
            
            
            //将密码和注册的手机号进行保存.临时保存最后上传到服务器
            self.dataHandler.phone = self.textPhone.text;
            self.dataHandler.password = MM;
            self.dataHandler.code = self.textCode.text;
            
            
//            NSLog(@"999999    %@ %@ %@",self.dataHandler.password,self.textCode.text,self.dataHandler.code);
            //跳转到第二注册页面
            
            Reg2ViewController *reg2 = [[Reg2ViewController alloc]initWithNibName:nil bundle:nil];
            
            [self presentViewController:reg2 animated:YES completion:nil];
            
            
//            [self.navigationController pushViewController:reg2 animated:YES];

            
        }else
        {
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            NSString *str = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"result_msg"]];
            UIAlertView *alter =[[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];

        
        
        
        }
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请检查网络！" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        

        
    }];



}





//获取验证码
-(void)getCode:(id)sender
{

    if (self.textPhone.text.length == 0) {
        
        [self.view makeToast:@"手机号码不能为空"];
        return;
    }
    
    
    //网络请求验证手机号以及密码
    NSString *urlStr = [NSString stringWithFormat:@"%@get_code",SERVER_ADDRESS];
    NSDictionary *parmas = @{@"wTel" : self.textPhone.text};
   
    [self.manager POST:urlStr parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        
        
        
//        NSLog(@"adhjasdhaksd  %@",responseObject);
        
        if ([responseObject[@"error_flag"] intValue] == 0) {
            
            self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(count:) userInfo:nil  repeats:YES];
            count = 60;

            self.btnCode.hidden = YES;
            self.countLabel.hidden = NO;
            self.countLabel.text = [NSString stringWithFormat:@"   获取中%i",count];
            self.countLabel.textColor = [UIColor lightGrayColor];
            self.countLabel.textAlignment = NSTextAlignmentCenter;
            self.countLabel.backgroundColor = [UIColor whiteColor];
            
        
        }
        
        else
        {
        
          NSString *str = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"result_msg"]];
            UIAlertView *alter =[[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];

        
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请检查网络!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
    }];
    

    
    

}


- (void)count:(id)sender{
    count--;
    self.countLabel.text = [NSString stringWithFormat:@"获取中%i",count];
    //[self.pwdBut setTitle:[NSString stringWithFormat:@"获取中%i",count] forState: UIControlStateDisabled];
    if (count==0) {
        [self.timer invalidate];
        self.countLabel.hidden = YES;
        self.btnCode.hidden = NO;
    }
}



#pragma 开始编辑的时候上拉键盘
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    if (ViewHeight == 480.0) {
        
        if (textField == self.textpassword1 || textField == self.textpassword2 || textField == self.textCode) {
       
        [UIView animateWithDuration:0.25 animations:^{
            if (self.view.y == 0) {
                self.view.y = -114;
            }
        }completion:^(BOOL finished){
        }];

        
    }
    }
    else if (ViewHeight == 568.0)
    {
        if (textField == self.textpassword2 || textField == self.textpassword1) {
            [UIView animateWithDuration:0.25 animations:^{
                if (self.view.y == 0) {
                    self.view.y = -114;
                }
            }completion:^(BOOL finished){
            }];

        }
    
    
    }
    
    return YES;



}




-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [self.textPhone resignFirstResponder];
    [self.textCode resignFirstResponder];
    
    [self.textpassword1 resignFirstResponder];
    [self.textpassword2 resignFirstResponder];
    
    
    [UIView animateWithDuration:0.25 animations:^{
        if (self.view.y == -114) {
            self.view.y = 0;
        }
    }completion:^(BOOL finished){
    }];

    
    
    return YES;
    
    
}



@end
